<?php


/** 获取区间内随机红包（符合正态分布）
 * @param $min 红包最小值
 * @param $max 红包最大值
 * @param $num 红包个数
 * @param $total 红包金额
 * @return array
 */
function redEnvelope($min, $max, $num, $total)
{

    $data = array();
    if ($min * $num > $total) {
        return array();
    }
    if ($max * $num < $total) {
        return array();
    }
    while ($num >= 1) {
        $num--;
        $kmix = max($min, $total - $num * $max);
        $kmax = min($max, $total - $num * $min);
        $kAvg = $total / ($num + 1);
        //获取最大值和最小值的距离之间的最小值
        $kDis = min($kAvg - $kmix, $kmax - $kAvg);
        //获取0到1之间的随机数与距离最小值相乘得出浮动区间，这使得浮动区间不会超出范围
        $r = ((float)(rand(1, 10000) / 10000) - 0.5) * $kDis * 2;

        $k = sprintf("%.2f", $kAvg + $r);
        $total -= $k;
        $data[] = $k;
    }
    return $data;
}
